package com.zros.modules.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zros.common.api.vo.Select;
import com.zros.modules.business.entity.Channel;
import com.zros.modules.business.entity.ChannelType;
import com.zros.modules.business.entity.Course;
import com.zros.modules.business.entity.Department;
import com.zros.modules.business.entity.Teacher;
import com.zros.modules.business.mapper.SelectMapper;
import com.zros.modules.business.service.ISelectService;
import com.zros.modules.system.entity.Organization;
import com.zros.modules.system.entity.User;

/**
 * <p>
 * 下拉框查询 服务实现类
 * </p>
 *
 * @author pangyinqiang
 * @since 2019-04-22
 */
@Service
@PropertySource("classpath:config.properties")
public class SelectServiceImpl extends ServiceImpl<SelectMapper, Select> implements ISelectService {
	
	/**
	 * 异常日志记录对象
	 */
	private static final Logger logger = LoggerFactory.getLogger(SelectServiceImpl.class);

	@Autowired
	private SelectMapper selectMapper;
	
	@Value("${query.department}")
	private String department;
	
	@Value("${query.course}")
	private String course;
	
	@Value("${query.channelType}")
	private String channelType;
	
	@Value("${query.channel}")
	private String channel;
	
	@Value("${query.manage}")
	private String manage;
	
	@Override
	public List<Select> queryOrgSelect(Map<String, String> selectMap) {
		List<Select> selectList = new ArrayList<Select>();

		List<Organization> orgList = selectMapper.queryOrgSelect(selectMap);
		
		for (Organization org : orgList) {
			Select select = new Select();
			select.setLabel(org.getOrganizationName());
			select.setValue(org.getId().toString());
			
			selectList.add(select);
		}
		return selectList;
	}
	
	@Override
	public List<Select> queryCourseSelectByOrgId(Long queryId,String queryKey) {
		List<Select> selectList = new ArrayList<Select>();
		Map<String, Long> selectMap = new HashMap<String, Long>(); 
		
		//queryKey为department，查询所有科系
		if(department.equals(queryKey))
		{
			//根据orgId查询出公共科系和当前私有科系
			selectMap.put("orgId", queryId);
			List<Department> depList = selectMapper.queryDepSelectByOrgId(selectMap);
			for (Department department : depList) {
				Select select = new Select();
				select.setLabel(department.getDepartmentName());
				select.setValue(department.getDepartmentId().toString());
				selectList.add(select);
			}
			return selectList;
		}
		//queryKey为course，根据科系id查询课程
		else if(course.equals(queryKey))
		{
			List<Course> courseList = selectMapper.queryCourseSelectByDepId(queryId);
			for (Course course : courseList) {
				Select select = new Select();
				select.setLabel(course.getCourseName());
				select.setValue(course.getCourseId().toString());
				selectList.add(select);
			}
			return selectList;
		}
		//queryKey为manage，根据orgId查询自身的科系 0查0的，别的查别的
		else if(manage.equals(queryKey))
		{
			//根据orgId查询自身科系
			selectMap.put("orgId", queryId);
			selectMap.put("manage", 1L);
			List<Department> depList = selectMapper.queryDepSelectByOrgId(selectMap);
			for (Department department : depList) {
				Select select = new Select();
				select.setLabel(department.getDepartmentName());
				select.setValue(department.getDepartmentId().toString());
				selectList.add(select);
			}
			return selectList;
		}
		//queryKey为其他，根据orgId查询所有科系和课程
		else
		{
			//1.根据orgId查询出公共科系和当前私有科系
			selectMap.put("orgId", queryId);
			List<Department> depList = selectMapper.queryDepSelectByOrgId(selectMap);
			
			//2.根据depId查询课程
			for (Department department : depList) {
				Long depId = department.getDepartmentId();
				List<Course> courseList = selectMapper.queryCourseSelectByDepId(depId);
				List<Select> childrenList = new ArrayList<Select>();
				
				for (Course course : courseList) {
					Select select = new Select();
					select.setLabel(course.getCourseName());
					select.setValue(course.getCourseId().toString());
					childrenList.add(select);
				}
				
				Select select = new Select();
				select.setLabel(department.getDepartmentName());
				select.setValue(depId.toString());
				select.setChildren(childrenList);
				selectList.add(select);
			}
			return selectList;
		}
	}
	
	@Override
	public List<Select> queryTeacherSelect(Map<String, Long> selectMap) {
		List<Select> selectList = new ArrayList<Select>();
		List<Teacher> teacherList = selectMapper.queryTeacherSelect(selectMap);
		
		for (Teacher teacher : teacherList) {
			Select select = new Select();
			select.setLabel(teacher.getTeacherName());
			select.setValue(teacher.getTeacherId().toString());
			selectList.add(select);
		}
		return selectList;
	}
	
	@Override
	public List<Select> queryUserSelectByOrgId(Long orgId) {
		List<Select> selectList = new ArrayList<Select>();
		List<User> userList = selectMapper.queryUserSelectByOrgId(orgId);
		
		for (User user : userList) {
			Select select = new Select();
			select.setLabel(user.getUserName());
			select.setValue(user.getId().toString());
			selectList.add(select);
		}
		return selectList;
	}
	
	@Override
	public List<Select> queryChannelSelectByOrgId(Long queryId,String queryKey) {
		List<Select> selectList = new ArrayList<Select>();
		Map<String, Long> selectMap = new HashMap<String, Long>(); 
		
		//queryKey为channelType，查询所有渠道类型
		if(channelType.equals(queryKey))
		{
			//根据orgId查询出公共渠道类型和当前私有的渠道类型
			selectMap.put("orgId", queryId);
			List<ChannelType> channelTypeList = selectMapper.queryChannelTypeSelectByOrgId(selectMap);
			for (ChannelType channelType : channelTypeList) {
				Select select = new Select();
				select.setLabel(channelType.getChannelTypeName());
				select.setValue(channelType.getChannelTypeId().toString());
				selectList.add(select);
			}
			return selectList;
		}
		//queryKey为channel，根据渠道类型id查询渠道
		else if(channel.equals(queryKey))
		{
			List<Channel> channelList = selectMapper.queryChannelSelectByTypeId(queryId);
			for (Channel channel : channelList) {
				Select select = new Select();
				select.setLabel(channel.getChannelName());
				select.setValue(channel.getChannelId().toString());
				selectList.add(select);
			}
			return selectList;
		}
		//queryKey为manage，根据orgId查询自身的渠道类型 0查0的，别的查别的
		else if(manage.equals(queryKey))
		{
			//根据orgId查询自身渠道类型
			selectMap.put("orgId", queryId);
			selectMap.put("manage", 1L);
			List<ChannelType> channelTypeList = selectMapper.queryChannelTypeSelectByOrgId(selectMap);
			for (ChannelType channelType : channelTypeList) {
				Select select = new Select();
				select.setLabel(channelType.getChannelTypeName());
				select.setValue(channelType.getChannelTypeId().toString());
				selectList.add(select);
			}
			return selectList;
		}
		//queryKey为其他，根据orgId查询所有渠道类型和渠道
		else
		{
			//1.根据orgId查询出公共渠道类型和当前私有的渠道类型
			selectMap.put("orgId", queryId);
			List<ChannelType> channelTypeList = selectMapper.queryChannelTypeSelectByOrgId(selectMap);
				
			//2.根据channelTypeId查询渠道
			for (ChannelType channelType : channelTypeList) {
				Long channelTypeId = channelType.getChannelTypeId();
				List<Channel> channelList = selectMapper.queryChannelSelectByTypeId(channelTypeId);
				List<Select> childrenList = new ArrayList<Select>();
				
				for (Channel channel : channelList) {
					Select select = new Select();
					select.setLabel(channel.getChannelName());
					select.setValue(channel.getChannelId().toString());
					childrenList.add(select);
				}
				
				Select select = new Select();
				select.setLabel(channelType.getChannelTypeName());
				select.setValue(channelType.getChannelTypeId().toString());
				select.setChildren(childrenList);
				selectList.add(select);
			}
			return selectList;
		}
	}
}
